<template>
  <div class="message">
    <div class="header">
      <div class="back" @click="goBack">
        <van-icon name="arrow-left" class="home-icon" />
      </div>
      <div class="title">Message Center</div>
    </div>

    <div class="message-list">
      <div
        v-for="(message, index) in messages"
        :key="index"
        class="message-item"
        @click="readMessage(message)"
        :class="{ unread: message.isRead == 0 }"
      >
        <div class="message-brief">
          <div class="message-title">{{ message.title }}</div>
          <div class="message-time">{{ getTimeIp(message.createdAt) }}</div>
        </div>
        <div class="divider-line"></div>
      </div>

      <div class="load-more"></div>
    </div>
    <van-popup v-model:show="show">
      <div class="popup-content">
        <div class="popup-title">{{ formData.title }}</div>
        <div
          class="popup-message"
          v-if="formData.content"
          v-html="formData.content"
        ></div>
        <div v-if="formData.imageUrl">
          <img class="message-img" :src="formData.imageUrl" alt="" />
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script setup name="Message">
import { reactive, ref } from "vue";
import { Icon } from "vant";
import { useRouter } from "vue-router";
import { UserApi } from "@/api/user.api";
import { getTimeIp } from "@/utils/calculator";
const router = useRouter();
const show = ref(false);
const messages = ref([]);
const goBack = () => {
  router.back();
};
const formData = reactive({
  title: "",
  content: "",
  imageUrl: "",
})
const readMessage = (messages) => {
  formData.title = messages.title;
  formData.content = messages.content;
  formData.imageUrl = messages.imageUrl;
  show.value = true;
  if (messages.isRead == 0) {
    messages.isRead = 1;
    UserApi.readMessage(messages.id);
  }
};
const init = () => {
  UserApi.getMessage({
    page: 0,
    size: 99999,
  }).then((res) => {
    if (res.code == 200) {
      messages.value = res.data.records;
    }
  });
};
init();
</script>

<style scoped>
.message {
  height: 100vh;
  background: #f5f5f5;
}
.header {
  position: relative;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  border-bottom: 1px solid #f5f5f5;
}
.unread {
  background: white;
  padding: 15px;
  cursor: pointer;
}
.unread::before {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  background: #ff4d4f;
  border-radius: 50%;
  position: absolute;
  left: 15px;
  margin-top: 5px;
}
.back {
  position: absolute;
  left: 15px;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.home-icon {
  width: 22px;
  height: 22px;
  font-weight: bold;
  margin-top: 6px;
}

.title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.message-list {
  padding: 10px;
}

.message-item {
  background: white;
  padding: 15px;
  cursor: pointer;
}

.message-brief {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  padding-left: 15px;
}

.message-title {
  font-size: 14px;
  color: #333;
  max-width: 50%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unread .message-title {
  font-weight: 600;
  color: #000;
}

.message-time {
  font-size: 12px;
  color: #999;
}

.divider-line {
  height: 1px;
  background: #f0f0f0;
  margin-top: 10px;
}

.load-more {
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #999;
  font-size: 12px;
}
.popup-content {
  padding: 15px;
  background: white;
}
.popup-title{
  text-align: center;
  max-width: 50%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin: 0 auto;
}
:deep(.van-popup--center){
  width: 80%;
  background: rgb(179, 242, 220);
  border-radius: 1rem;
  margin-bottom: .4rem;
  border: .02667rem solid rgb(195, 227, 213);
  padding: 15px;
}
:deep(.popup-content){
  background: rgb(179, 242, 220);
  padding: 0;
}
.message-img{
  width: 100%;
  max-height: 200px;
  object-fit: contain;
  margin-bottom: 10px;
  border-radius: .5rem;
}
</style>
